package com.fortune.controller;

import com.fortune.base.BaseAPIExcution;
import com.fortune.common.paging.PagingData;
import com.fortune.constants.Constants;
import com.fortune.dto.excution.MStoreOutExcution;
import com.fortune.dto.in.mStoreOut.MStoreOutListDto;
import com.fortune.entity.MUserInfo;
import com.fortune.enums.MStoreOutEnum;
import com.fortune.service.MStoreOutService;
import com.fortune.vo.mStoreOut.MStoreOutInfo;
import com.fortune.vo.mStoreOut.MStoreOutInfoDetail;
import com.fortune.vo.mStoreOut.MStoreOutInfoDetailIn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(value = "mStoreOut", description = "厂商仓库出库", tags = {"厂商仓库出库"})
@RestController
@RequestMapping("mStoreOut")
public class MStoreOutController {
	@Autowired
	private MStoreOutService mStoreOutService;
	
	@ApiOperation(value = "出库单列表", nickname = "mStoreOutList", notes = "", tags={ "@石匡" })
	@ApiImplicitParams({
			@ApiImplicitParam(name="page", value="第几页",paramType="query",dataType="int"),
			@ApiImplicitParam(name="length", value="每页容量",paramType="query",dataType="int"),
			@ApiImplicitParam(name="search", value="搜索关键词",paramType="query",dataType="string"),
			@ApiImplicitParam(name="type",value="是否已发货（0已发货1未发货）",paramType="query",dataType="int",allowableValues = "0,1")
	})
    @GetMapping(value = "list",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<PagingData<MStoreOutInfo>> list(HttpServletRequest request, @ApiIgnore @Validated MStoreOutListDto mStoreOutListDto) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		mStoreOutListDto.setPage((mStoreOutListDto.getPage()-1)*10);
		List<MStoreOutInfo> list = mStoreOutService.queryMStoreOutList(companyId,mStoreOutListDto);
		int count = mStoreOutService.queryMStoreOutListCount(companyId,mStoreOutListDto);
		PagingData<MStoreOutInfo> pagingData = new PagingData<>();
		pagingData.setData(list);
		pagingData.setRecordsTotal(count);
		return new MStoreOutExcution<PagingData<MStoreOutInfo>>(pagingData, MStoreOutEnum.SUCCESS);
	}
	
	@ApiOperation(value = "出库单详情", nickname = "mStoreOutDetail", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="出库单id",paramType="query",dataType="string",required=true)
    @GetMapping(value = "detail",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<MStoreOutInfoDetail> detail(HttpServletRequest request,@Validated @NotBlank(message="出库单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		MStoreOutInfoDetail detail = mStoreOutService.queryMStoreOutInfoDetail(companyId,id);
		return new MStoreOutExcution<MStoreOutInfoDetail>(detail, MStoreOutEnum.SUCCESS);
	}


	@ApiOperation(value = "验收出库", nickname = "mStoreOutConfirm", notes = "", tags={ "@石匡" })
	@ApiImplicitParam(name="id", value="出库单id",paramType="query",dataType="string",required=true)
    @PutMapping(value = "confirm",
        produces = { MediaType.APPLICATION_JSON_VALUE })
	public BaseAPIExcution<Void> confirm(HttpServletRequest request,@Validated @NotBlank(message="出库单id不能为空")String id) {
		HttpSession session = request.getSession();
		MUserInfo mUserInfo = (MUserInfo) session.getAttribute(Constants.SESSION_M_USER);
		String companyId = mUserInfo.getmId();
		MStoreOutInfoDetailIn detail = mStoreOutService.queryMStoreOutInfoDetailIn(companyId,id);
		int num = mStoreOutService.updateMStoreOutShipType(companyId,detail,id);
		if (num < 1){
			return new MStoreOutExcution<Void>(null, MStoreOutEnum.FAILURE);
		}
		return new MStoreOutExcution<Void>(null, MStoreOutEnum.SUCCESS);
	}
}
